1. /* expupto.cpp by K.Tsuru */
  2. /************************************
  3. One of auxiliary tools for SN library
  4. *************************************/
  5. #ifndef DEF_CONST_H
  6. #include "defconst.h"
  7. #endif // DEF_CONST_H
  8. #ifndef TOOLS_H
  9. #include "tools.h"
  10. #endif //TOOLS_H
  11. // It returns the number of figures n!.
  12. static double Stirling(double n){
  13. static const double C1 = 0.5 * log10(2 * M_PI) +1.0, C2 = log10(M_E);
  14. return C1 - C2 * n + (n + 0.5) * log10(n);
  15. }
  16. static double fig, log10X;
  17. static double fx(double n) {
  18. return fig + n * log10X - Stirling(n);
  19. }
  20. /**************************************************
  21. It returns how many terms needs to evaluate exp(x) in a
  22. given precision.
  23. x^n
  24. ----- = 10^(-f)
  25. n!
  26. i.e.
  27. f + n log10(x) - log10(n!) = 0
  28. parameter log10x : Prease give log10(x).
  29. ***************************************************/
  30. long upToExpSeries(long precision, double log10x) {
  31. fig = precision; log10X = log10x;
  32. if ( fx(1.0) < 0) return 1L; // x < 10^(-f)
  33. return (long)(bisection(1, precision, 1.0e-5, fx) + 1.0);
  34. }
  35. /// a test program
  36. #if 0
  37. static long upToExp1(long precision) {
  38. return upToExpSeries(precision, 0.0);
  39. }
  40. int main() {
  41. long L;
  42. for (L = 500; L < 5000; L += 500) {
  43. long p = upToExp1(L);
  44. printf("%ld : %g, p = %ld, %g, %g\n", L, Stirling((double)p), p, fx(p-1), fx(p));
  45. }
  46. L = 1000;
  47. for (double x = 1.0; x > 1.0e-200; x /= 1.0e10) {
  48. long p = upToExpSeries(L, log10(x));
  49. printf("%ld : p = %ld, %g, %g (x = %g)\n", L, p, fx(p-1), fx(p), x);
  50. }
  51. return 0;
  52. }
  53. #endif

expupto.cpp : last modifiled at 2015/09/06 15:57:00(1,565 bytes)
created at 2016/04/11 11:17:20
The creation time of this html file is 2017/10/07 10:54:15 (Sat Oct 07 10:54:15 2017).